package cn.liutao.order.api;

import cn.liutao.common.entity.LoginUser;
import cn.liutao.common.entity.Validation;
import cn.liutao.common.utils.ServletUtils;
import cn.liutao.common.utils.ValidUtil;
import cn.liutao.order.entity.param.OrderConfirmParam;
import cn.liutao.order.entity.param.OrderCreateParam;
import cn.liutao.order.entity.vo.OrderVo;
import cn.liutao.order.service.OrderService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.groups.Default;
import java.io.IOException;

/**
 * @author liutao
 * @Description 订单确认接口
 * @date 2023/11/4
 */
@WebServlet("/order/confirm")
public class OrderConfirmServlet extends HttpServlet {

    private OrderService orderService;

    @Override
    public void init() throws ServletException {
        orderService = OrderService.getInstance();
    }

    /**
     * 订单确认接口，用户点击直接购买或者从购物车点击去支付按钮后返回构造的订单信息
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        OrderConfirmParam confirmParam = ServletUtils.getBodyParam(req, OrderConfirmParam.class);
        ValidUtil.validated(confirmParam, Default.class);
        Integer userId = LoginUser.getUserId();
        OrderVo orderVo = orderService.confirmOrder(confirmParam,userId);
        ServletUtils.ok(resp,orderVo);
    }
}
